#coding:utf-8
'''
Created on 2016-7-5

@author: root
'''
from flask import make_response,redirect,request, session
from gameplat.worker import api
from gameplat.utils.cache import cache
from example.utils.toolfunc import getSecretCookie, is_in_weixin,\
                        is_in_baidu_mobile, is_in_qq_mobile, check_platform,\
                        is_in_weibo_mobile
from example.utils.config import USER_COOKIE_KEY
from urllib2 import quote
from example.utils.authclient import authclient
from example.utils.baiduclient import bdclient
from example.utils.qqauthclient import qqclient
from example.utils.sinaclient import sinaclient

cache = cache

class add_api_resource:
    """装饰器方法，指定API的资源地址
    """
    
    def __init__(self,*args,**kw):
        """初始化装饰器参数
        """
        self.args = args
        self.kwards = kw
        
    def __call__(self,resource):
        """添加资源到路径中
        """
        api.add_resource(resource,*self.args,**self.kwards)
        return resource
    
class cache_reponse:
    """请求结果缓存
    """
    def __init__(self,cache_key,exprise=0,keywords=[]):
        self.cache_key = cache_key
        self.exprise = exprise
        self.keywords = keywords
        
    def __call__(self,func):
        """缓存请求结果
        """
        def f(*args,**kwargs):
            cache_key = "response_cache:%s"%self.cache_key
            keyword_key = []
            for keyword in self.keywords:
                _k = "%s_%s"%(keyword,args[1].get(keyword))
                keyword_key.append(_k)
            if keyword_key:
                cache_key = "%s:%s"%(cache_key,":".join(keyword_key))
            response_cache = cache.get(cache_key)
            if response_cache:
                return make_response(response_cache)
            response = func(*args,**kwargs)
            cache_lock_key = "%s:lock"%cache_key
            if cache.incr(cache_lock_key,1)==1:
                cache.set(cache_key,response.data)
                if self.exprise:
                    cache.expire(cache_key,self.exprise)
                    cache.expire(cache_lock_key,self.exprise)
            return response
        return f
    
    
def login_required(func):
    """判断是否登录
    """
    def f(*args,**kwargs):
        access_token = getSecretCookie(USER_COOKIE_KEY)
        if access_token:
            response = make_response(func(*args,**kwargs))
            return response
        _bkurl = request.url
        host = request.host
        scheme = request.scheme
        s = check_platform()
        print s
        if is_in_weixin():
            bkurl = "%s://%s/auth/wechat_auth?bkurl=%s"%(scheme,host,quote(str(_bkurl),safe=""))
            wechat_auth_url = authclient.product_auth_url(bkurl)
            return redirect(wechat_auth_url)
        elif is_in_baidu_mobile():
            bkurl = "%s://%s/auth/baidu_auth?bkurl=%s"%(scheme,host,quote(str(_bkurl),safe=""))
            session['redirect_url_bak'] = bkurl
            baidu_auth_url = bdclient.create_authorize_url(bkurl)
            return redirect(baidu_auth_url)
        elif is_in_qq_mobile():
            bkurl = "%s://%s/auth/qq_auth?bkurl=%s"%(scheme,host,quote(str(_bkurl),safe=""))
            session['redirect_url_bak'] = bkurl
            qq_auth_url = qqclient.create_authorize_url(bkurl)
            print 123123123
            print session['redirect_url_bak']
            return redirect(qq_auth_url)
        elif is_in_weibo_mobile():
            bkurl = "%s://%s/auth/sina_auth"%(scheme,host)
            session['redirect_url_bak'] = bkurl
            session['return_jump_url'] = _bkurl
            wibo_auth_url = sinaclient.create_authorize_url(bkurl)
            print session['redirect_url_bak']
            print wibo_auth_url
            return redirect(wibo_auth_url)
        else:
            bkurl = "%s://%s/auth/baidu_auth?bkurl=%s"%(scheme,host,quote(str(_bkurl),safe=""))
            session['redirect_url_bak'] = bkurl
            baidu_auth_url = bdclient.create_authorize_url(bkurl)
            return redirect(baidu_auth_url)
    f.__name__=func.__name__
    return f


    
    